package db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import cool.tianyou.blue.bird.model.Message;
import cool.tianyou.blue.bird.model.po.Sms;

public class MessageHelper extends SQLiteOpenHelper {
    public MessageHelper(@Nullable Context context) {
        super(context, "blue-bird", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createMessageTable = "create table  message(\n" +
                "    id integer primary key  autoincrement ,\n" +
                "    content text,\n" +
                "    parsed integer,\n" +
                "    parse_content text,\n" +
                "    read integer,\n" +
                "    read_time integer,\n" +
                "    receive_time integer,\n" +
                "    category text,\n" +
                "    tag text,\n" +
                "    type integer,\n" +
                "    session_id integer\n" +
                ")";
        db.execSQL(createMessageTable);
        String sql = "create table session(" +
                "    id                 integer primary key autoincrement,\n" +
                "    number             text,\n" +
                "    least_receive_time integer,\n" +
                "    least_content      text,\n" +
                "    least_category     text,\n" +
                "    least_tag          text\n" +
                ")";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insertSms(Sms sms) {
        //写session表
//        String sql = "select count(*) from session where number ='" + sms.getSource() + "'";
//        SQLiteDatabase readableDatabase = getReadableDatabase();
//        Cursor query = readableDatabase.query("session", new String[]{"count(*)"}, "number = ?", new String[]{sms.getSource()}, null, null, null);
//        query.moveToFirst();
//        boolean exist = query.getInt(0) == 1;
//
//
//
//
//
//
//        String insertSQL = "insert into  message(content, parsed, parse_content, read, read_time, receive_time, category, tag, type)\n" +
//                "values ('%s',%d,'%s',%d,%d,%d,'%s','%s',%d)";
//        getWritableDatabase()
//                .execSQL(String.format(insertSQL, sms.getSource(), sms.getContent(),
//                        sms.getParsed(), sms.getParseContent(), sms.getRead(),
//                        sms.getReceiveTime(), sms.getCategory(), sms.getTag(), sms.getType()));
    }
}
